clear
*set mem 600m
set more off
set logtype text
set matsize 800
cap program drop _all

cap log close

use $histdir/ICPSR_1900_agcensus.dta, clear
keep state fips
gen statefip = floor(fips/1000)
rename state stateicp
drop fips
drop if statefip == . | statefip == 0
duplicates drop
save $tempdir\statefip_to_stateicp.dta, replace

use $histdir/ICPSR_1900_agcensus.dta, clear

keep state county name totpop farms farmown farmten farmout farmlab farmfert totval farmval farmbui farmequi animsold animslau

/* Income: value of farm products not fed to livestock (farmout) + value of house rent food & fuel (rentfoodfuel) 

	rentfoodfuel/farmout = 0.266 (Goldenweiser)
	
  Expenditure: labor & fertilizer (farmlab + farmfert) + feed seed & threshing (feedseedthresh) + taxes (tax) + maintenance (mtnce)
  
	(feedseedthresh)/(farmlab + farmfert)  = 0.765 (Goldenweiser)
	tax = 0.006*totval 
	mtnce = 0.05*farmbui + 0.15*farmequi
	
*/

drop if county==0 | state>73

gen soldslau = animsold + animslau

gen farmout_adj = farmout - 0.25*soldslau

gen rentfoodfuel = 0.2663934*farmout_adj
gen feedseedthresh = 0.7653061*(farmlab + farmfert)
gen tax = 0.006*totval
gen mtnce = (0.05*farmbui + 0.15*farmequi)

gen farmrev = farmout_adj + rentfoodfuel
gen farmexp = farmlab + farmfert + feedseedthresh + tax + mtnce

gen farm_net_income = (farmrev - farmexp)/farms

gen interest = 0.05*totval

egen farmown_state = total(farmown), by(state)
egen farmown_usa = total(farmown)

gen farmout_pf = farmout/farms
gen rentfoodfuel_pf = rentfoodfuel/farms
gen mtnce_pf = mtnce/farms
gen farmlab_pf = (farmlab)/farms
gen farmfert_pf = farmfert/farms
gen tax_pf = tax/farms
gen feedseedthresh_pf = feedseedthresh/farms
gen farmout_adj_pf = farmout_adj/farms

gen farmrev_pf = farmrev/farms
gen farmexp_pf = farmexp/farms
gen soldslau_pf = soldslau/farms 
gen interest_pf = interest/farms

foreach v in farmout_adj rentfoodfuel feedseedthresh tax mtnce farmrev farmexp farmlab farmfert interest {
	egen `v'_state = total(`v'*farmown/farms), by(state)
	replace `v'_state = `v'_state/farmown_state
	egen `v'_usa = total(`v'*farmown/farms)
	replace `v'_usa = `v'_usa/farmown_usa
}


gen farm_net_income_state = farmrev_state - farmexp_state
gen farm_net_income_usa = farmrev_usa - farmexp_usa

preserve

keep state county name farmout_adj_pf rentfoodfuel_pf farmlab_pf farmfert_pf feedseedthresh_pf tax_pf mtnce_pf farm_net_income interest_pf
foreach v in farmout_adj rentfoodfuel farmlab farmfert feedseedthresh tax mtnce interest {
	rename `v'_pf `v'
}

gen farmer_net_income = farm_net_income - interest

order state county name farmer_net_income farm_net_income interest farmout_adj rentfoodfuel farmlab farmfert feedseedthresh tax mtnce
save $tempdir\farmers_income_county.dta, replace

restore

egen x = tag(state)
keep if x==1
drop x

keep state farmout_adj_state rentfoodfuel_state feedseedthresh_state tax_state mtnce_state farmlab_state farmfert_state farm_net_income_state farm_net_income_usa interest_state
foreach v in farmout_adj rentfoodfuel farmlab farmfert feedseedthresh tax mtnce interest farm_net_income {
	rename `v'_state `v'
}

gen farmer_net_income = farm_net_income - interest 

rename state stateicp
mer 1:1 stateicp using $tempdir\statefip_to_stateicp.dta, keepus(statefip)
keep if _merge==3
drop _merge

order stateicp statefip farmer_net_income farm_net_income interest  farmout_adj rentfoodfuel farmlab farmfert feedseedthresh tax mtnce farm_net_income_usa 

save $tempdir\farmers_income_state.dta, replace

use $altdir\occ_wage_PH.dta, clear

keep occ1950 occname1950 occwage_PH_all

drop if occwage_PH_all==.

expand 100
sort occ1950
gen stateicp = 1 if occ1950!=occ1950[_n-1]
replace stateicp = stateicp[_n-1] + 1 if occ1950==occ1950[_n-1]

mer m:1 stateicp using $tempdir\statefip_to_stateicp.dta, keepus(statefip)
drop if _merge==1
drop _merge

order stateicp statefip occ1950 occname occwage_PH_all

mer m:1 statefip using $tempdir\farmers_income_state.dta, keepus(farmer_net_income farm_net_income)
drop if _merge==1
drop _merge

gen occwage1900 = occwage_PH_all if occ1950!=100
replace occwage1900 = farmer_net_income if occ1950==100

gen occwage1900_v2 = occwage1900
replace occwage1900_v2 = farm_net_income if occ1950==100

sort occ1950 statefip

save $tempdir\occ_wage_bystate.dta, replace











